package com.battery.web.controller.system.battery;

import java.math.BigDecimal;
import java.util.List;

import com.battery.common.utils.ali.AliPayEntity;
import com.battery.framework.config.properties.AliPayResource;
import com.battery.system.domain.BO.OrderBO;
import com.battery.system.domain.VO.OrderVO;
import com.battery.system.service.battery.IOrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Order;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 支付订单Controller
 *
 * @author battery
 * @date 2021-02-04
 */
@Controller
@RequestMapping("/system/order")
public class OrderController extends BaseController {
    private String prefix = "system/order";

    @Autowired
    private AliPayResource aliPayResource;

    private AliPayEntity _callApi() {
        AliPayEntity entity = new AliPayEntity();
        entity.setAppId(aliPayResource.getAppId());
        entity.setAlipayPublicKey(aliPayResource.getAlipayPublicKey());
        entity.setCharset(aliPayResource.getCharset());
        entity.setGatewayUrl(aliPayResource.getGatewayUrl());
        entity.setMerchantPrivateKey(aliPayResource.getMerchantPrivateKey());
        entity.setSignType(aliPayResource.getSignType());
        entity.setNotifyUrl(aliPayResource.getNotifyUrl());
        entity.setPayeeLogonId(aliPayResource.getPayeeLogonId());
        entity.setPayeeUserId(aliPayResource.getPayeeUserId());
        entity.setFreezeNotifyUrl(aliPayResource.getFreezeNotifyUrl());
        entity.setServiceId(aliPayResource.getServiceId());
        return entity;
    }


    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order() {
        return prefix + "/order";
    }

    /**
     * 查询支付订单列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderBO orderBO) {
        startPage();
//        List<Order> list = orderService.selectOrderList(orderBO);
        List<OrderVO> list = orderService.selectOrderVOList(orderBO);
        return getDataTable(list);
    }

    /**
     * 导出支付订单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "支付订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order) {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 新增支付订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存支付订单
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "支付订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Order order) {
        return toAjax(orderService.insertOrder(order));
    }

    /**
     * 修改支付订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Order order = orderService.selectOrderById(id);
        mmap.put("order", order);
        return prefix + "/edit";
    }

    /**
     * 修改保存支付订单
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除支付订单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "支付订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }


    /**
     * 跳转到退款页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/refundMoney/{id}")
    public String refundMoney(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/refundMoney";
    }


    /**
     * 进行退款操作
     *
     * @param order
     * @return
     */
    @RequiresPermissions("system:order:refundMoney")
    @Log(title = "支付订单退款", businessType = BusinessType.UPDATE)
    @PostMapping("/refundMoney")
    @ResponseBody
    public AjaxResult refundMoney(Order order) {

        if (StringUtils.isBlank(order.getId())) {
            return error("支付订单ID为空");
        }

        if (order.getRefundMoney() == null || order.getRefundMoney().compareTo(BigDecimal.ZERO) <= 0) {
            return error("退款金额不能为空，或者小于等于0");
        }

        if (StringUtils.isBlank(order.getRefundDesc())) {
            return error("请填写退款描述");
        }

//        Order currentOrder = orderService.selectByPrimaryKey(order.getId());
        AliPayEntity aliPayEntity = _callApi();
        return orderService.backstageRefundMoney(order, aliPayEntity);
    }


    /**
     * 退押金操作
     *
     * @param id 支付订单ID
     * @return
     */
    @RequiresPermissions("system:order:refundDeposit")
    @Log(title = "支付订单退押金", businessType = BusinessType.OTHER)
    @PostMapping("/refundDeposit")
    @ResponseBody
    public AjaxResult refundDeposit(String id) {
        if (StringUtils.isBlank(id)) {
            return error("主键ID为空");
        }
        AliPayEntity aliPayEntity = _callApi();
        return orderService.backstageRefundDeposit(id, aliPayEntity);
    }

}
